package com.now.printer.adapter;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.now.printer.R;
import com.now.printer.base.BaseAdapter.BaseAdapter;
import com.now.printer.base.BaseAdapter.BaseHolder;
import com.now.printer.bean.FileBean;
import com.now.printer.db.RecentlyTable;
import com.now.printer.utils.FileUtils;

import org.xutils.common.util.DensityUtil;
import org.xutils.common.util.LogUtil;
import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.io.File;
import java.util.List;

/**
 * Created by xudong on 2021/5/16.
 */
public class RecentlyFileListAdapter extends BaseAdapter<RecentlyTable> {
    private OnItemClickListener mOnItemClickListener = null;

    private int fragmentType = -1;
    public RecentlyFileListAdapter(Context context, List<RecentlyTable> data, int layoutId,int fragmentType) {
        super(context, data, layoutId);
        this.fragmentType = fragmentType;
    }

    public void setOnItemClickLitener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    @Override
    public void onBind(BaseHolder holder, RecentlyTable item, int position) {
        TextView itemName = holder.getView(R.id.item_name);
        ImageView itemIcon = holder.getView(R.id.item_icon);
        ImageView ivCollect = holder.getView(R.id.iv_collect);

        LinearLayout lr = holder.getView(R.id.lr);

        if (item != null) {
            if (item.getName() == null)return;

            itemName.setText(item.getName());
//            itemIcon.setImageResource(item.iconId);

            if (FileUtils.isPicFile(item.getName())) {
                x.image().bind(holder.getView(R.id.item_icon), new File(item.getPath()).toURI().toString(), new ImageOptions.Builder()
                        .setSize(DensityUtil.dip2px(120), DensityUtil.dip2px(120))
                        .setRadius(DensityUtil.dip2px(5))
                        // 如果ImageView的大小不是定义为wrap_content, 不要crop.
                        .setCrop(true) // 很多时候设置了合适的scaleType也不需要它.
                        // 加载中或错误图片的ScaleType
                        //.setPlaceholderScaleType(ImageView.ScaleType.MATRIX)
                        .setImageScaleType(ImageView.ScaleType.CENTER_CROP)
                        .setLoadingDrawableId(R.mipmap.ic_launcher)
                        .setFailureDrawableId(R.mipmap.ic_launcher)
                        .setIgnoreGif(false)
                        .build());
            } else {
                itemIcon.setImageResource(item.getImageId());
            }

            ivCollect.setImageResource(Boolean.parseBoolean(item.getIsCollected())?R.mipmap.star:R.mipmap.unstar);
            LogUtil.i("适配器获取到是否收藏： " + item.getIsCollected());

            ivCollect.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemClickListener != null) {
                        mOnItemClickListener.onItemClickCollect(position,!Boolean.parseBoolean(item.getIsCollected()));
                    }
                }
            });
            if (fragmentType == 2){
                ivCollect.setVisibility(View.GONE);
            }

            lr.setOnClickListener(v -> {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(position, item.getName());
                }
            });
        }else {
            LogUtil.i("adapter item is null");
        }
    }

    public interface OnItemClickListener {
        void onItemClick(int position, String type);
        void onItemClickCollect(int position,boolean isCollect);
    }
}

